package pers.chao.tool.http.util.future;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * An enhanced {@link Future} that supports callback and attachments
 * 增强型Future，增加了监听和回调
 */
public interface IFuture extends Future<Object> {

	Object getNow();

	boolean isSuccessful();

	Throwable cause();

	/**
	 * 添加监听
	 *
	 * @param listener
	 * @return
	 */
	IFuture addListener(IFutureListener listener);

	/**
	 * 添加监听和回调
	 *
	 * @param listener
	 * @param callback
	 * @return
	 */
	IFuture addListener(IFutureListener listener, Executor callback);

	IFuture removeListener(IFutureListener listener);

	IFuture await() throws InterruptedException;

	IFuture awaitUntilComplete();

	boolean await(long timeout, TimeUnit unit) throws InterruptedException;

	boolean awaitUntilComplete(long timeout, TimeUnit unit);

	Object getAttachment(String name);

	Map<String, Object> getAttachments();

	void setAttachment(String name, Object value);
}
